CreateMealTicketRemovalCommandHandler.execute   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 20
c 0
b 0
f 0
rs 9.6
cc 3
1
import { CommandHandler } from '@nestjs/cqrs';
2
import { Inject } from '@nestjs/common';
3
import { IMealTicketRemovalRepository } from 'src/Domain/HumanResource/MealTicket/Repository/IMealTicketRemovalRepository';
4
import { MealTicketRemoval } from 'src/Domain/HumanResource/MealTicket/MealTicketRemoval.entity';
5
import { MealTicketRemovalAlreadyExistException } from 'src/Domain/HumanResource/MealTicket/Exception/MealTicketRemovalAlreadyExistException';
6
import { IsMealTicketRemovalAlreadyExist } from 'src/Domain/HumanResource/MealTicket/Specification/IsMealTicketRemovalAlreadyExist';
7
import { CreateMealTicketRemovalCommand } from './CreateMealTicketRemovalCommand';
8
import { IDateUtils } from 'src/Application/IDateUtils';
9
import { NotAWorkingDateException } from 'src/Domain/HumanResource/MealTicket/Exception/NotAWorkingDateException';
10
11
@CommandHandler(CreateMealTicketRemovalCommand)
12
export class CreateMealTicketRemovalCommandHandler {
13
  constructor(
14
    @Inject('IMealTicketRemovalRepository')
15
    private readonly mealTicketRemovalRepository: IMealTicketRemovalRepository,
16
    @Inject('IDateUtils')
17
    private readonly dateUtils: IDateUtils,
18
    private readonly isMealTicketRemovalAlreadyExist: IsMealTicketRemovalAlreadyExist
19
  ) {}
20
21
  public async execute(command: CreateMealTicketRemovalCommand): Promise<void> {
22
    const { date, comment, user } = command;
23
24
    if (false === this.dateUtils.isAWorkingDay(new Date(date))) {
25
      throw new NotAWorkingDateException();
26
    }
27
28
    if (
29
      true ===
30
      (await this.isMealTicketRemovalAlreadyExist.isSatisfiedBy(
31
        user,
32
        new Date(date)
33
      ))
34
    ) {
35
      throw new MealTicketRemovalAlreadyExistException();
36
    }
37
38
    await this.mealTicketRemovalRepository.save([
39
      new MealTicketRemoval(date, user, comment)
40
    ]);
41
  }
42
}
43